package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.Work;
import com.ketangpai.admin.entity.dto.WorkDTO;
import com.ketangpai.admin.service.WorkService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作业表(Work)表控制层
 *
 * @author makejava
 * @since 2023-06-14 01:16:49
 */
@RestController
@RequestMapping("work")
@Api(tags = "作业管理")
public class WorkController {
    /**
     * 服务对象
     */
    @Resource
    private WorkService workService;

    /**
     * 通过主键查询单条数据
     */
    @ApiOperation("根据id查询")
    @GetMapping("/selectById/{id}")
    public JsonResult<?> selectById(@PathVariable Integer id) {
        Work work = workService.queryById(id);
        return JsonResult.success(work);
    }

    /**
     * 通过课程ID查询作业
     */
    @ApiOperation("通过课程ID查询作业")
    @GetMapping("/queryByCid/{id}")
    public JsonResult<?> queryByCid(@PathVariable Integer id) {
        List<WorkDTO> works = workService.queryByCid(id);
        System.out.println("works:"+works);
        return JsonResult.success(works);
    }

    /**
     * 新增数据
     */
    @ApiOperation("添加")
    @PostMapping("/insert")
    public JsonResult<?> insert( Work work) {
        System.out.println("work:"+work);
        boolean result = workService.insert(work);
        if (result) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    public JsonResult<?> delete(@PathVariable Integer id) {
        boolean result = workService.deleteById(id);
        if (result) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public JsonResult<?> update(Work work) {
        boolean result = workService.update(work);
        if (result) {
            return JsonResult.success("编辑成功");
        }
        return JsonResult.error("编辑失败");
    }
}

